﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.getemisionvaluecorporateactionnext" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;
using System.Text;

namespace SIPO.Web.AjaxHandler.Loaddata
{
    public class getemisionvaluecorporateactionnext : SIPO.Web.BaseHandler
    {
        recTariffRepo recTarifDal = new recTariffRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recTransactionTypeRepo transTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            recTransactionType transType = transTypeDal.GetByLikeTransTypeName("aksi korporasi");

            string isFormula = string.Empty;            
            decimal? invoiceValue = 0;
            string tariffId = string.Empty;
            string impositionBaseID = string.Empty;

            int corporateActionTypeID = Convert.ToInt32(context.Request.Params["CorporateActionTypeID"]);
            decimal? nilaiEmisi = (!string.IsNullOrEmpty(context.Request.Params["NilaiEmisi"])) ? Helper.ToDecimal(context.Request.Params["NilaiEmisi"], System.Globalization.NumberStyles.Number, Helper.Culture.Indonesian) : 0m;
            int subSubSectorId = (!string.IsNullOrEmpty(context.Request.Params["SubSubSectorId"]) ? Convert.ToInt32(context.Request.Params["SubSubSectorId"]) : 0);

            recTariff tarif = recTarifDal.GetByCorporateActionType(transType.TransTypeID, corporateActionTypeID, subSubSectorId);

            if (tarif != null)
            {
                tariffId = tarif.TariffID.ToString();
                impositionBaseID = tarif.ImpositionBaseID.HasValue ? tarif.ImpositionBaseID.Value.ToString() : string.Empty;
                isFormula = tarif.IsFormula == true ? "1" : "0";
                decimal? value = tarif.Value;

                decimal? minvalue = tarif.MinValue;
                decimal? maxvalue = tarif.MaxValue;
                decimal? percentage = tarif.Percentage;
                decimal? result = (nilaiEmisi * percentage) / 100;

                if (tarif.IsFormula)
                {
                    if (result > maxvalue)
                        invoiceValue = maxvalue;
                    else if (result <= maxvalue && result >= minvalue)
                        invoiceValue = result;
                    else
                        invoiceValue = minvalue;
                }
                else
                    invoiceValue = value;
                
            }
            
            StringBuilder sbProject = new StringBuilder();
            sbProject.Append("{");
            sbProject.AppendFormat("  {0}InvoiceValue{0}:{0}{1}{0}", "\"", invoiceValue);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}TariffID{0}:{0}{1}{0}", "\"", tariffId);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}IsFormula{0}:{0}{1}{0}", "\"", isFormula);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}ImpositionBaseID{0}:{0}{1}{0}", "\"", impositionBaseID);
            sbProject.Append("}");

            context.Response.Write(sbProject.ToString());
            
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}